usbip: Fix misuse of strncpy()
authorBen Hutchings <ben@decadent.org.uk>
Fri, 20 Jul 2018 00:30:24 +0000 (01:30 +0100)
committerSalvatore Bonaccorso <carnil@debian.org>
Thu, 13 Feb 2020 05:14:49 +0000 (05:14 +0000)
Bug-Debian: https://bugs.debian.org/897802
Forwarded: https://marc.info/?l=linux-usb&m=153213915806258

gcc 8 reports:

usbip_device_driver.c: In function ‘read_usb_vudc_device’:
usbip_device_driver.c:106:2: error: ‘strncpy’ specified bound 256 equals destination size [-Werror=stringop-truncation]
  strncpy(dev->path, path, SYSFS_PATH_MAX);
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
usbip_device_driver.c:125:2: error: ‘strncpy’ specified bound 32 equals destination size [-Werror=stringop-truncation]
  strncpy(dev->busid, name, SYSFS_BUS_ID_SIZE);
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I'm not convinced it makes sense to truncate the copied strings here,
but since we're already doing so let's ensure they're still null-
terminated.  We can't easily use strlcpy() here, so use snprintf().

usbip_common.c has the same problem.

Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
Gbp-Pq: Topic bugfix/all
Gbp-Pq: Name usbip-fix-misuse-of-strncpy.patch

tools/usb/usbip/libsrc/usbip_common.c
tools/usb/usbip/libsrc/usbip_device_driver.c

index b8d7d480595a8477db79c090cd46ee2f671a4e56..02aae7cccb2dafbd015ffc9e8fd0b6fb1e375873 100644 (file)
@@ -226,9 +226,9 @@ int read_usb_device(struct udev_device *sdev, struct usbip_usb_device *udev)
        path = udev_device_get_syspath(sdev);
        name = udev_device_get_sysname(sdev);
 
-       strncpy(udev->path,  path,  SYSFS_PATH_MAX - 1);
+       snprintf(udev->path, SYSFS_PATH_MAX - 1, "%s", path);
        udev->path[SYSFS_PATH_MAX - 1] = '\0';
-       strncpy(udev->busid, name, SYSFS_BUS_ID_SIZE - 1);
+       snprintf(udev->busid, SYSFS_BUS_ID_SIZE - 1, "%s", name);
        udev->busid[SYSFS_BUS_ID_SIZE - 1] = '\0';
 
        sscanf(name, "%u-%u", &busnum, &devnum);
index 927a151fa9aa9e52c5c721f20c34d1682575b17f..fd3fb32b68232023117b78693e6a30dd3c22f433 100644 (file)
@@ -93,7 +93,7 @@ int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev)
        copy_descr_attr16(dev, &descr, idProduct);
        copy_descr_attr16(dev, &descr, bcdDevice);
 
-       strncpy(dev->path, path, SYSFS_PATH_MAX - 1);
+       snprintf(dev->path, SYSFS_PATH_MAX - 1, "%s", path);
        dev->path[SYSFS_PATH_MAX - 1] = '\0';
 
        dev->speed = USB_SPEED_UNKNOWN;
@@ -113,7 +113,7 @@ int read_usb_vudc_device(struct udev_device *sdev, struct usbip_usb_device *dev)
        dev->busnum = 0;
 
        name = udev_device_get_sysname(plat);
-       strncpy(dev->busid, name, SYSFS_BUS_ID_SIZE - 1);
+       snprintf(dev->busid, SYSFS_BUS_ID_SIZE - 1, "%s", name);
        dev->busid[SYSFS_BUS_ID_SIZE - 1] = '\0';
        return 0;
 err: